//
//  MaterHeaderView.m
//  HandWork
//
//  Created by 王阳阳 on 16/1/21.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MaterHeaderView.h"
#import "ShopModel.h"
#import "CenterCollectionViewCell.h"
#import "CollectionHeaderViewController.h"
#import "AppDelegate.h"
#import "EredarViewController.h"
#import "CailiaoHotViewController.h"
@interface MaterHeaderView ()<UICollectionViewDataSource, UICollectionViewDelegate>
{
    NSInteger _page;
}
@property (nonatomic, retain)NSMutableArray *dateArr;//数据数组
@property (nonatomic, retain)UICollectionView *butcoll;//collectionView
@property (nonatomic, retain)UICollectionViewFlowLayout *butlayout;//布局
@end
@implementation MaterHeaderView

- (void)dealloc
{
    [_dateArr release];
    [_butcoll release];
    [_butlayout release];
    [super dealloc];
}
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self creat];
    }
    return self;
}
#pragma mark - collectionView

- (void)creat
{
    //数据
    [self data];
    //layout 布局
    self.butlayout = [[UICollectionViewFlowLayout alloc]init];
    //行列边距
    self.butlayout.minimumLineSpacing = 0;
    self.butlayout.minimumInteritemSpacing = 0;
    //边距
    self.butlayout.itemSize = CGSizeMake(self.frame.size.width / 4, 140);
    //滑动方向
    self.butlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //collectionView的创建
    self.butcoll = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 150) collectionViewLayout:_butlayout];
    self.butcoll.backgroundColor = [UIColor whiteColor];//背景颜色
    //代理
    self.butcoll.dataSource = self;
    self.butcoll.delegate = self;
    [self addSubview:self.butcoll];//添加
    //重用池
    [_butcoll registerClass:[CenterCollectionViewCell class] forCellWithReuseIdentifier:@"butcell"];
    [_butcoll release];
    [_butlayout release];
   
}
#pragma mark - collectionView必须实现的方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CenterCollectionViewCell *butcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"butcell" forIndexPath:indexPath];
    ShopModel *s = _dateArr[indexPath.row];
    [butcell.bgimg sd_setImageWithURL:[NSURL URLWithString:s.pic]];//图片
    butcell.namelabel.text = s.name;//标题
    return butcell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dateArr.count;
    //    return 10;
}
#pragma mark - 数据解析
- (void)data
{
    NSString *str = @"http://m.shougongke.com/index.php?c=Shiji&vid=15";
   [AFNnetWork GETwithURL:str completion:^(id result) {
       NSArray *arr = result[@"data"][@"hot"];
       self.dateArr = [ShopModel modelHandlerWithArray:arr];
       [_butcoll reloadData];
   }];
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate *app = [[UIApplication sharedApplication] delegate];
    if (indexPath.item == 0) {
        //新品页面
        CollectionHeaderViewController *clt = [[CollectionHeaderViewController alloc]init];
        [app.secondnavi pushViewController:clt animated:YES];
    }
    if (indexPath.item == 1) {
        //热门页面
        CailiaoHotViewController *hot = [[CailiaoHotViewController alloc]init];
        [app.secondnavi pushViewController:hot animated:YES];
    }
    if (indexPath.item ==2) {
        //达人推荐页面
        EredarViewController *eredar = [[EredarViewController alloc]init];
        [app.secondnavi pushViewController:eredar animated:YES];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
